﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using FortuneArterial.Site.BusinessLogicLayer.LunceneSearch.Service;
using FortuneArterial.Site.BusinessLogicLayer.LunceneSearch.Analysis;
using System.Web;

namespace FortuneArterial.Site.Laboratory.Controllers.Lucene.Net
{
    [DisplayName("文件索引")]
    public partial class LuceneMusicFileController
        : BaseController
    {
        [HttpGet]
        [DisplayName("文件索引")]
        public ActionResult DirectoryFile()
        {
            return SmartView(new Models.FileEntity());
        }

        [HttpPost]
        public ActionResult DirectoryFileData(string q, string filed, int page = 1, int rows = 10)
        {
            // perform Lucene search
            var models = String.IsNullOrEmpty(q) ? lucene.GetAllIndexRecords() : lucene.SearchDefault(q, filed);
            return Json(new
            {
                total = models.Count(),
                rows = models.Skip((page - 1) * rows).Take(rows).OrderBy(m => m.FileName)
            });
        }

        [HttpPost]
        [ActionName("DirectoryFile")]
        public ActionResult Post(string path)
        {
            //Fix Bug: 没有权限访问系统文件夹中的内容，这些文件夹只会存在与分区根目录
            var topDirs = System.IO.Directory.EnumerateDirectories(path, "*", SearchOption.TopDirectoryOnly)
                    .Where(m => !m.ToUpper().EndsWith("$RECYCLE.BIN") && !m.ToUpper().EndsWith("SYSTEM VOLUME INFORMATION"))
                    .ToList();
            topDirs.Insert(0, path);

            foreach (var topDir in topDirs)
            {
                lucene.AddUpdateLuceneIndex(
                    System.IO.Directory.EnumerateFiles(topDir, "*.*",
                        topDir == path ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories)
                        .Select(CreateEntity)
                    );
            }

            return Json(new { Result = true });
        }

        private Models.FileEntity CreateEntity(string path)
        {
            var ext = Path.GetExtension(path);
            Models.FileEntity model;
            switch (ext.ToLower())
            {
                case ".mp3":
                    model = new Models.MusicFileEntity(path);
                    break;
                default:
                    model = new Models.FileEntity(path);
                    break;
            }

            return model;
        }

        [HttpDelete]
        [ActionName("DirectoryFile")]
        public ActionResult Delete(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                lucene.ClearLuceneIndex();
            }
            else
            {
                lucene.ClearLuceneIndexRecord(id);
            }

            return Json(new { Result = true });
        }

        [HttpPut]
        [ActionName("DirectoryFile")]
        public ActionResult Put(string id)
        {
            return Json(new { Result = true });
        }

        private LuceneService<Models.FileEntity, PanGuAnalyzer> lucene =
            new LuceneService<Models.FileEntity, PanGuAnalyzer>(
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"temp\lucene_index\all_file\"));
    }
}